package edu.must.uh.util;

import java.util.Date;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;

public class TaskManager implements ServletContextListener {

    private static Logger logger = Logger.getLogger(TaskManager.class);
    
    
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        Calendar calendar = Calendar.getInstance();
        Date defaultdate = calendar.getTime();        
        Timer qTimer = new Timer();
        qTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    SqlUtils.getMySqlInstance().getlist("SELECT now() FROM base_user limit 1");
                    logger.info("mysql heart beat");
                }
                catch (SQLException e) {                  
                    logger.error("mysql heart beat - failed",e);
                }
            }
        }, defaultdate, 5 * 60 * 1000);

    }

}
